# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Smeltzov <andrew.smeltzov@gmail.com>

_realname=shiboken2
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-qt5)
pkgdesc="CPython bindings generator for C++ libraries (mingw-w64)"
pkgver=5.15.2
pkgrel=8
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://doc.qt.io/qtforpython-5/"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
         "${MINGW_PACKAGE_PREFIX}-libxslt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-numpy"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=('staticlibs' 'strip')
_pkgfqn=pyside-setup-opensource-src-$pkgver
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/${_pkgfqn}.tar.xz
        001-shiboken.patch
        002-cmake-relative-paths.patch
        003-llvm-paths.patch)
sha256sums=('b306504b0b8037079a8eab772ee774b9e877a2d84bab2dbefbe4fa6f83941418'
            'd7150cd5c554e5da68f5a53e08b46af02e45c5fc3481e99a1289b194e3220015'
            'e8fce91fee692a3ef906c841575e53e141a58c0c97c63fd1d94bed5a823190f9'
            '0cb712ba34d9951b0c29dc3b71f9c82882064331bd54a4cf0f4c792837932497')

prepare() {
  cd "${srcdir}"/$_pkgfqn
  patch -p1 -i ${srcdir}/001-shiboken.patch
  patch -p1 -i ${srcdir}/002-cmake-relative-paths.patch
  patch -p1 -i ${srcdir}/003-llvm-paths.patch
}

build() {
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/clang.exe \
    -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/clang++.exe \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_TESTS=OFF \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    ../${_pkgfqn}/sources/shiboken2

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  local _PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${_f}"
  done

  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/Shiboken2-${pkgver}/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done

  # Install egg-info
  cd "${srcdir}"/$_pkgfqn
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=shiboken2
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r shiboken2.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}
